package com.itheima.xiancheng.demo3;

public class Demo3 {
    public static void main(String[] args) {
        Desk desk = new Desk();
        int count = 7;
        //生产者
        new Thread(() -> {
            synchronized (desk) {
                for (int i = 1; i < count; i++) {
                    if (desk.isFlag()) {
                        try {
                            desk.wait();
                            i--;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        desk.put(i);
                        desk.setFlag(true);
                        desk.notify();

                    }
                }


            }
        }).start();

        new Thread(() -> {
            int eatCount=0;
            synchronized (desk) {
                while (eatCount <= count) {
                    if (desk.isFlag()) {
                        desk.take();
                        eatCount++;
                        desk.setFlag(false);
                        desk.notify();
                    } else {
                        try {
                            desk.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}
